package com.yunfei.api.v2.service;
import com.yunfei.api.v2.param.ReserveNoParam;
import com.yunfei.api.v2.param.SignZuoFeiXieYiParam;
import com.yunfei.api.v2.result.QiXiaoDianZiQianShuIsCancel.QuXiaoQianShuLiuChengIsCancelResult;
import com.yunfei.api.v2.result.zuoFeiXieYiSign.ZuoFeiXieYiSignResult;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.HeTongSignInfoGetStatusResultStatus;
import com.yunfei.enums.ResultStatus;
import com.yunfei.mapper.aiiidb.HeTongSignInfoMapper;
import com.yunfei.utils.string.StringUtil;
import com.yunfei.yunhetong.elecContBeneAcctDtl.bo.ElecContBeneAcctDtlBo;
import com.yunfei.yunhetong.elecContBeneAcctDtl.service.ElecContBeneAcctDtlService;
import com.yunfei.yunhetong.elecContCancelDtl.bo.ElecContCancelDtlBo;
import com.yunfei.yunhetong.elecContCancelDtl.service.ElecContCancelDtlService;
import com.yunfei.yunhetong.elecContOperLog.bo.ElecContOperLogBo;
import com.yunfei.yunhetong.elecContOperLog.service.ElecContOperLogService;
import com.yunfei.yunhetong.heTongSignManager.entity.HeTongSignManagerEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class HTSQuXiaoQianShuService {
    @Autowired
    private HeTongSignInfoMapper heTongSignInfoMapper;
    @Autowired
    private ElecContOperLogService elecContOperLogService;
    @Autowired
    private ElecContCancelDtlService elecContCancelDtlService;
    @Autowired
    private ElecContBeneAcctDtlService elecContBeneAcctDtlService;
    /**
    * @Description: 取消电子合同签署流程（理财师）
    * @Author: zhengyunfei
    * @Date: 2019/8/26
    * @time:14:20
    */
    public ResultModel isCancel(ReserveNoParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveNo=param.getReserveNo();//预约编号
            if(StringUtil.isNullOrEmpty(reserveNo)){
                resultModel.setCode(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getCode());
                resultModel.setMessage(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getMessage());
                return resultModel;
            }
            queryMap.put("reserveNo",reserveNo);
          //保存数据
            QuXiaoQianShuLiuChengIsCancelResult result=heTongSignInfoMapper.isCancel(queryMap);
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
    /**
     * @Description: 取消作废
     * @Author: zhengyunfei
     * @Date: 2019/8/26
     * @time:14:20
     */
    public ResultModel cancel(ReserveNoParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveNo=param.getReserveNo();//预约编号
            if(StringUtil.isNullOrEmpty(reserveNo)){
                resultModel.setCode(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getCode());
                resultModel.setMessage(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getMessage());
                return resultModel;
            }
            queryMap.put("reserveNo",reserveNo);
            //保存数据
            heTongSignInfoMapper.cancel(queryMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
    /**
     * @Description: 作废签署判断
     * @Author: zhengyunfei
     * @Date: 2019/8/26
     * @time:14:20
     */
    public ResultModel zuoFeiSignPanDuan(ReserveNoParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveNo=param.getReserveNo();//预约编号
            if(StringUtil.isNullOrEmpty(reserveNo)){
                resultModel.setCode(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getCode());
                resultModel.setMessage(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getMessage());
                return resultModel;
            }
            queryMap.put("reserveNo",reserveNo);
            //作废协议签署功能判断
           ZuoFeiXieYiSignResult zuoFeiXieYiSignResult= heTongSignInfoMapper.getZuoFeiSign(queryMap);
            resultModel.setData(zuoFeiXieYiSignResult);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
    /**
     * @Description: 签署作废协议
     * @Author: zhengyunfei
     * @Date: 2019/8/26
     * @time:14:20
     */
    public ResultModel signZuoFeiXieYi(SignZuoFeiXieYiParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveNo=param.getReserveNo();//预约编号
            String contractId=param.getContractId();
            if(StringUtil.isNullOrEmpty(reserveNo)){
                resultModel.setCode(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getCode());
                resultModel.setMessage(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getMessage());
                return resultModel;
            }
            queryMap.put("reserveNo",reserveNo);
            //更新合同签署状态
            ElecContCancelDtlBo updateParam=new ElecContCancelDtlBo();
            //HeTongSignManagerEntity heTongSignManagerEntity=new HeTongSignManagerEntity();
           // heTongSignManagerEntity.setContractSts("5");
            //heTongSignManagerEntity.setcReserveno(reserveNo);
           // heTongSignManagerEntity.setContractId(contractId);
            updateParam.setContractSts("5");
            updateParam.setcReserveno(reserveNo);;
            elecContCancelDtlService.updateStatus(updateParam);
            System.out.println("客户签署作废协议更新状态开始。。。。。。。。。。。。。。。。。。。。。"+contractId);
            ElecContBeneAcctDtlBo elecContBeneAcctDtlBo=new ElecContBeneAcctDtlBo();
            elecContBeneAcctDtlBo.setCReserveno(reserveNo);
            elecContBeneAcctDtlService.updateStatus5(elecContBeneAcctDtlBo);
            System.out.println("客户签署作废协议更新状态完成。。。。。。。。。。。。。。。。。。。。。");

            //插入日志,sunzong签署作废协议的时候，调用这个更改状态，他又调用了记录日志的接口，所以这里去掉记录日志
           /* ElecContOperLogBo elecContOperLogBo=new ElecContOperLogBo();
            elecContOperLogBo.setContractId(param.getContractId());
            elecContOperLogBo.setContractNum("");
            elecContOperLogBo.setOperId(param.getCustNo());
            elecContOperLogBo.setOperTyp("客户签署作废协议成功");
            elecContOperLogService.add(elecContOperLogBo);*/
            resultModel.setData("操作成功");
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }

}
